package com.ts.api.module.protocol.service.impl;

import com.gitee.apanlh.util.base.StringUtils;
import com.gitee.apanlh.util.date.DateUtils;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMaskDataExchange;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5DataExchange;
import com.ts.api.module.channel.entity.po.ChannelUserRegisterPO;
import com.ts.api.module.protocol.dao.UserProtocolDao;
import com.ts.api.module.protocol.entity.po.UserProtocolPO;
import com.ts.api.module.protocol.entity.qo.UserProtocolQO;
import com.ts.api.module.protocol.service.UserProtocolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserProtocolServiceImpl implements UserProtocolService {

    @Autowired
    private UserProtocolDao userProtocolDao;

    @Override
    @Transactional(readOnly = true)
    public List<UserProtocolPO> list(UserProtocolQO userProtocolQO) {
        return userProtocolDao.list(userProtocolQO);
    }

    @Override
    public int insert(UserProtocolPO userProtocolPO) {
        return userProtocolDao.insert(userProtocolPO);
    }

    @Override
    public String appendTemplate(ChannelUserRegisterPO channelUserRegisterPO, String protocolTemplate, RespPlatformPhoneMd5DataExchange apiPlatFormDataExchange) {
        //  用户基础信息
        String userName = channelUserRegisterPO.getUserName();
        String phone = channelUserRegisterPO.getPhone();
        String idcard = channelUserRegisterPO.getIdcard();
        String createTime = DateUtils.currentTime("yyyy年MM月dd日");
        String companyName = apiPlatFormDataExchange.getDisplayName();
        String productName = apiPlatFormDataExchange.getProductName();

        String text = StringUtils.replace(protocolTemplate, "#{userName}", userName);
        text = StringUtils.replace(text, "#{phone}", phone);
        text = StringUtils.replace(text, "#{idcard}", idcard == null ? "" : idcard);
        text = StringUtils.replace(text, "#{date}", createTime);
        text = StringUtils.replace(text, "#{companyName}", companyName);
        text = StringUtils.replace(text, "#{productName}", productName);
        return text;
    }

    @Override
    public String appendTemplatePhoneMask(ChannelUserRegisterPO channelUserRegisterPO, String protocolTemplate, RespPlatformPhoneMaskDataExchange phoneMaskDataExchange) {
        //  用户基础信息
        String userName = channelUserRegisterPO.getUserName();
        String phone = channelUserRegisterPO.getPhone();
        String idcard = channelUserRegisterPO.getIdcard();
        String createTime = DateUtils.currentTime("yyyy年MM月dd日");
        String companyName = phoneMaskDataExchange.getDisplayName();
        String productName = phoneMaskDataExchange.getProductName();

        String text = StringUtils.replace(protocolTemplate, "#{userName}", userName);
        text = StringUtils.replace(text, "#{phone}", phone);
        text = StringUtils.replace(text, "#{idcard}", idcard == null ? "" : idcard);
        text = StringUtils.replace(text, "#{date}", createTime);
        text = StringUtils.replace(text, "#{companyName}", companyName);
        text = StringUtils.replace(text, "#{productName}", productName);
        return text;
    }

}
